<%@page import="de.tuhrig.sportlog.helper.LogToHTMLHelper"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="de.tuhrig.sportlog.pmf.PMF" %>
<%@ page import="de.tuhrig.sportlog.user.log.Log" %>
<%@ page import="de.tuhrig.sportlog.pmf.queries.QueryFactory" %>
<%@ page import="de.tuhrig.sportlog.helper.CalendarView" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.DateFormat" %>
<%@ page import="com.cj.htmlcal.HtmlCalendar" %>

<html>
	<head>
		<%@ include file="controls/Meta.jsp" %>
	</head>
	
	<%!
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		
		DateFormat sdf0 = new SimpleDateFormat("HH:mm a");
		DateFormat sdf1 = new SimpleDateFormat("HH 'hours' mm 'minutes'");
		DateFormat sdf2 = new SimpleDateFormat("EEE, MMM d, ''yy");
	%>
	
	<div id='container'>
		<%@ include file="controls/Header.jsp" %>
		<%@ include file="controls/Left.jsp" %>
		<%@ include file="controls/Right.jsp" %>
		
		<div id='content'>	
			<%
				if(user != null) {

					pm = PMF.get().getPersistenceManager();
			
					query = new QueryFactory(pm).countAllLogsFromUserId(); 
					
					int numberOflogsFromUser = (Integer) query.execute(user.getUserId());
			%>
	        
	        	<h1>Log Information</h1>
	        	<h2>Overview</h2>
	        
				<table align="center" width="300">
					<tr>
						<td nowrap><%= new CalendarView().getHTML() %></td>
					</tr>
				</table> 

	        	<ul>
					<li><%= numberOflogsFromUser %> personal stored logs</li>
				</ul>
				<hr/>
				<h2>Details</h2>
				<%
					int from = 0;
					int to = from + 5;

					if(request.getParameter("from") != null) {
							
						from = Integer.parseInt(request.getParameter("from").toString());
						to = from + 5;
					}
					
					List<Log> logsFromUser;
					
					String day = request.getParameter("day");
					String year = request.getParameter("year");
					String month = request.getParameter("month");
					
					if(day != null && year != null && month != null) {
											
						query = new QueryFactory(pm).getLogsFromUserIdFromDate(from, to);
						 
						Calendar tmp = Calendar.getInstance();
						tmp.set(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)-1);
	
						Date d1 = tmp.getTime();
						 
						tmp.add(Calendar.DAY_OF_MONTH, 1);
						
						Date d2 = tmp.getTime();
						
						logsFromUser = (List<Log>) query.execute(user.getUserId(), d1, d2);
					}
					else {
					
						query = new QueryFactory(pm).getLogs(from, to);
						logsFromUser = (List<Log>) query.execute(user.getUserId());
					}

					dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
				
					for(Log log: logsFromUser) {

				%>
						<h3>Log (from <%= sdf2.format(log.getDate()) %>)</h3>
						
							<table width="100%">
								<tr>
									<td>
										<%= new LogToHTMLHelper(log).getVerticalIconOverview() %>
									</td>
								</tr>
								<tr>
									<td>
										<div id="logoverviewlist">
											<ul>
												<li><b>Key:</b> <%= log.getKey() %></li>
												<li><b>Date:</b> <%= sdf2.format(log.getDate()) %></li>
								
												<%
													if(log.getStart() != null && log.getEnd() != null) {
												%>
												<li><b>Start:</b> <%= sdf0.format(log.getStart()) %></li>
												<li><b>End:</b> <%= sdf0.format(log.getEnd()) %></li>
												
												<%
													}
												%>
												
												<li><b>Duration:</b> <%= sdf1.format(log.getDuration()) %></li>
												<li><b>Location:</b> <%= log.getLocation() %></li>
												<li><b>Sport:</b> <%= log.getSport() %></li>
												
												<% 
													if(log.getWeather() != null) {
												%>
												
												<li><b>Temperature:</b> <%= log.getWeather().getTemperature() %></li>
												<li><b>Condition:</b> <%= log.getWeather().getCondition() %></li>
												
												<%
													}
												%>
											</ul>
										</div>
									</td>
								</tr>
							</table>
							
						<a href="">More</a> <a href="">Edit</a> <a href="">Remove</a>
				<% 
					}
				%>
				
				<p align="center">
					<%
						if(from > 0) {
							
							if(day != null && year != null && month != null) {
					%>		
								<a href="Logs.jsp?from=<%= from - 5 %>&day=<%= day%>&year=<%= year%>&month=<%= month%>"><< Back</a>
					<% 
							}
							else {
					%>
								<a href="Logs.jsp?from=<%= from - 5 %>"><< Back</a>
					<%	
							}
						}
					%>
					<%
						if(numberOflogsFromUser > 5) {
							
							if(day != null && year != null && month != null) {	
					%>		
								<a href="Logs.jsp?from=<%= to %>&day=<%= day%>&year=<%= year%>&month=<%= month%>">Next >></a>
					<%
							}
							else {
					%>
								<a href="Logs.jsp?from=<%= to %>">Next >></a>
					<%	
							}
						}
					%>
				</p>
	        <%
					query.closeAll();
			
					pm.close();
	        	}
			%>
		</div>
		<%@ include file="controls/Footer.jsp" %>
	</div>
</html>